package com.example.yueliang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.yueliang.common.Result;
import com.example.yueliang.dto.ViewDetailDto;
import com.example.yueliang.pojo.Favorite;
import com.example.yueliang.pojo.View;
import com.example.yueliang.service.impl.FavoriteServiceImpl;
import com.example.yueliang.service.impl.ViewServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/view")
public class ViewController {
    @Autowired
    private ViewServiceImpl viewService;

    @Autowired
    private FavoriteServiceImpl favoriteService;

    /**
     * 页面未完全加载  处理请求
     * @param viewId
     * @return
     */
    @GetMapping("/detail")
    public Result queryByViewId(HttpServletRequest request,@RequestParam Integer viewId){
        LambdaQueryWrapper<View> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(View::getViewId,viewId);
        View view = viewService.getOne(queryWrapper);

        ViewDetailDto viewDetailDto = new ViewDetailDto();
        BeanUtils.copyProperties(view,viewDetailDto);

        //根据viewid查询该景点所有收藏的人数
        LambdaQueryWrapper<Favorite> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Favorite::getViewId,viewId);
        int count = favoriteService.count(queryWrapper1);
        viewDetailDto.setCount(count);

        //根据uid和viewid查询
        int uid = (int) request.getSession().getAttribute("user");
        LambdaQueryWrapper<Favorite> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Favorite::getViewId,viewId);
        queryWrapper2.eq(Favorite::getUid,uid);
        Favorite one = favoriteService.getOne(queryWrapper2);

        if (one != null){
            viewDetailDto.setFlag(1);
        }else {
            viewDetailDto.setFlag(0);
        }


        return Result.success(viewDetailDto);
    }

    /**
     * 点击收藏按钮后  处理请求
     * @param request
     * @param action
     * @param viewId
     * @return
     */
    @GetMapping("/favorite")
    public Result favorite(HttpServletRequest request, @RequestParam String action, @RequestParam Integer viewId){
        if (action.equals("add")) {
            // 执行添加操作
            Favorite favorite = new Favorite();
            int uid = (int) request.getSession().getAttribute("user");
            favorite.setViewId(viewId);
            favorite.setUid(uid);
            favorite.setTime(LocalDateTime.now());
            favoriteService.save(favorite);

            return Result.success("收藏成功");
        } else if (action.equals("remove")) {
            // 执行移除操作
            LambdaQueryWrapper<Favorite> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Favorite::getViewId,viewId);
            favoriteService.remove(queryWrapper);

            return Result.success("取消收藏");
        }
        return null;
    }

    /**
     * 查询用户收藏的景点
     */
    @PostMapping("/collections")
    public Result queryCollectViews(HttpServletRequest request){
        LambdaQueryWrapper<Favorite> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Favorite::getUid,request.getSession().getAttribute("user"));
        List<Favorite> favoriteList = favoriteService.list(queryWrapper1);

        List<View> viewList = new ArrayList<>();
        for (Favorite favorite : favoriteList){
            Integer viewId = favorite.getViewId();
            LambdaQueryWrapper<View> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(View::getViewId,viewId);
            View view = viewService.getOne(queryWrapper2);
            viewList.add(view);
        }

        return Result.success(viewList);
    }
}
